/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.water.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.water.domain.WaterQualityMonitor;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.water.service.WaterQualityMonitorService;
import me.zhengjie.water.domain.vo.WaterQualityMonitorQueryCriteria;
import me.zhengjie.water.mapper.WaterQualityMonitorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author Meorin
* @date 2024-11-11
**/
@Service
@RequiredArgsConstructor
public class WaterQualityMonitorServiceImpl extends ServiceImpl<WaterQualityMonitorMapper, WaterQualityMonitor> implements WaterQualityMonitorService {

    private final WaterQualityMonitorMapper waterQualityMonitorMapper;

    @Override
    public PageResult<WaterQualityMonitor> queryAll(WaterQualityMonitorQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(waterQualityMonitorMapper.findAll(criteria, page));
    }

    @Override
    public List<WaterQualityMonitor> queryAll(WaterQualityMonitorQueryCriteria criteria){
        return waterQualityMonitorMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(WaterQualityMonitor resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(WaterQualityMonitor resources) {
        WaterQualityMonitor waterQualityMonitor = getById(resources.getRecordId());
        waterQualityMonitor.copy(resources);
        saveOrUpdate(waterQualityMonitor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<WaterQualityMonitor> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WaterQualityMonitor waterQualityMonitor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("PH值", waterQualityMonitor.getPh());
            map.put("水温", waterQualityMonitor.getTemperature());
            map.put("溶解氧含量", waterQualityMonitor.getDissolvedOxygen());
            map.put("化学需氧量", waterQualityMonitor.getChemicalOxygenDemand());
            map.put("氨氮含量", waterQualityMonitor.getAmmoniaNitrogenContent());
            map.put("记录时间", waterQualityMonitor.getRecordTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<WaterQualityMonitor> getYearlyData(Integer year) {
        // 使用QueryWrapper构建查询条件
        QueryWrapper<WaterQualityMonitor> queryWrapper = new QueryWrapper<>();

        // 构建该年份的起止时间
        LocalDateTime startOfYear = LocalDateTime.of(year, 1, 1, 0, 0, 0);
        LocalDateTime endOfYear = LocalDateTime.of(year, 12, 31, 23, 59, 59);

        // 转换为Timestamp
        Timestamp startTimestamp = Timestamp.valueOf(startOfYear);
        Timestamp endTimestamp = Timestamp.valueOf(endOfYear);

        // 添加时间范围条件
        queryWrapper.ge("record_time", startTimestamp)
                .le("record_time", endTimestamp)
                .orderByAsc("record_time");

        return this.list(queryWrapper);
    }
}